package org.itboys.admin.service.lasted;

import org.apache.commons.lang.StringUtils;
import org.itboys.admin.dao.SundayAreaMapper;
import org.itboys.admin.entity.lasted.SundayArea;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


/**
 * 包名：com.hz.sunday.tianjie.entity.mysql.product
 * 作者 : 江雄
 * Email: empty90911@126.com
 * 时间：2016年3月10日 下午2:52:46
 * 描述: 圣代_省（直辖市）/市/区or县/街道or乡镇_service。四级行政区划
 */


@Service
public class SundayAreaService {
    /**
     * 项目启动时缓存省（直辖市）/市/区or县/街道or乡镇数据
     */
    public static List<SundayArea> all_provinces;
    public static List<SundayArea> all_citys;
    public static List<SundayArea> all_districts;
    public static List<SundayArea> all_towns;
    public static boolean isTemp = false;//是否读取缓存中数据

    @Autowired
    private SundayAreaMapper areaMapper;
    //获取树结构省市区。

    /**
     * 获取省（直辖市）/市/区or县/街道or乡镇。树结构类型数据
     *
     * @param isTown。是否包含乡镇
     * @return
     */
    public List<SundayArea> getAreaTree(boolean isTown) {
        if (!isTemp) {
            Map<String, Object> param = new HashMap<String, Object>();
            param.put("grades", Arrays.asList(new Integer[]{1, 2, 3, 4}));
            List<SundayArea> areas = areaMapper.select(param);
            List<SundayArea> _all_provinces = new ArrayList<>();
            List<SundayArea> _all_citys = new ArrayList<>();
            List<SundayArea> _all_districts = new ArrayList<>();
            List<SundayArea> _all_towns = new ArrayList<>();
            for (SundayArea area : areas) {
                if (area.getGrade() == 1) {
                    _all_provinces.add(area);
                } else if (area.getGrade() == 2) {
                    _all_citys.add(area);
                } else if (area.getGrade() == 3) {
                    _all_districts.add(area);
                } else if (area.getGrade() == 4) {
                    _all_towns.add(area);
                }
            }
            all_provinces = _all_provinces;
            all_citys = _all_citys;
            all_districts = _all_districts;
            all_towns = _all_towns;
        }
        for (SundayArea province : all_provinces) {
            List<SundayArea> citys = new ArrayList<SundayArea>();
            for (SundayArea city : all_citys) {
                //匹配市
                if (city.getParentId().longValue() == province.getId().longValue()) {
                    List<SundayArea> districts = new ArrayList<SundayArea>();
                    for (SundayArea district : all_districts) {
                        //匹配区县
                        if (district.getParentId().longValue() == city.getId().longValue()) {

                            List<SundayArea> towns = new ArrayList<SundayArea>();
                            if (isTown) {
                                for (SundayArea town : all_towns) {
                                    //匹配街道乡镇
                                    if (town.getParentId().longValue() == district.getId().longValue()) {
                                        towns.add(town);
                                    }
                                }
                                district.setChildren(towns);
                            }
                            districts.add(district);
                        }
                    }
                    city.setChildren(districts);
                    citys.add(city);
                }
            }
            province.setChildren(citys);
        }
        return all_provinces;
    }


    /**
     *
     *2016年12月20日-------------------静态获取
     */
    /**
     * 获取省。启动时已缓存
     *
     * @return
     */
    public List<SundayArea> getProvince() {
        return all_provinces;
    }

    /**
     * 获取市。启动时已缓存
     *
     * @param provinceId
     * @return
     */
    public List<SundayArea> getCity(Long provinceId) {

        List<SundayArea> citys = new ArrayList<SundayArea>();
        for (SundayArea city : all_citys) {
            if (city.getParentId().longValue() == provinceId.longValue()) {
                citys.add(city);
            }
        }
        return citys;
    }

    /**
     * 获取区县。启动时已缓存
     *
     * @param cityId
     * @return
     */
    public List<SundayArea> getDistrict(Long cityId) {
        List<SundayArea> districts = new ArrayList<SundayArea>();
        for (SundayArea district : all_districts) {
            if (district.getParentId().longValue() == cityId.longValue()) {
                districts.add(district);
            }
        }
        return districts;
    }


    /**
     * 获取街道乡镇
     *
     * @param districtId
     * @return
     */
    //获取街道乡镇
    public List<SundayArea> getTown(Long districtId) {
        List<SundayArea> towns = new ArrayList<>();
        for (SundayArea town : all_towns) {
            if (town.getParentId().longValue() == districtId.longValue()) {
                towns.add(town);
            }
        }
        return towns;
    }

    /**
     * 获取所有市（拼装拼音首字母）
     *
     * @return
     */
    public List<Map<String, Object>> getCityByPy() {
        List<Map<String, Object>> parents = new ArrayList<>();
        if (!isTemp) {
            Map<String, Object> param = new HashMap<String, Object>();
            param.put("grade", 2);
            List<SundayArea> citys = areaMapper.select(param);
            //拼装拼音首字母
            Set<String> pyszms = new TreeSet<>(new PyszmComparator());
            for (SundayArea city : citys) {
                pyszms.add(city.getPyszm());
            }
            for (String pyszm : pyszms) {

                Map<String, Object> parent = new HashMap<>();
                parent.put("id", 0);
                parent.put("pyszm", pyszm);
                parent.put("name", pyszm);
                List<Map<String, Object>> children = new ArrayList<>();
                for (SundayArea city : citys) {
                    if (StringUtils.isNotEmpty(city.getPyszm()) &&
                            city.getPyszm().equalsIgnoreCase(pyszm)) {
                        Map<String, Object> child = new HashMap<String, Object>();
                        child.put("id", city.getId());
                        child.put("pyszm", city.getPyszm());
                        child.put("name", city.getName());
                        children.add(child);
                    }

                }
                parent.put("children", children);
                parents.add(parent);
            }
        }
        return parents;
    }

    class PyszmComparator implements Comparator<String> {
        @Override
        public int compare(String o1, String o2) {

            //int result = Integer.valueOf(o1)-Integer.valueOf(02);
            return o1.compareTo(o2);
        }
    }


}
